#ifndef FILE_H
#define FILE_H

#include <fstream>
#include <iostream>
#include <cstring>
#include <QString>
#include <QTextStream>
using namespace std;

class Personal {
public:
    Personal();
    Personal(char*, char*, char*, int, long);
    void writeToFile(fstream&) const;
    void readFromFile(fstream&);
    void readKey(QString input);
    int size() const {
        return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
    }
    // 比较SSN来判断用户输入的SSN是否存在对应人的记录
    bool operator==(const Personal& pr) const {
        return strncmp(pr.SSN, SSN, 9) == 0;
    }
    istream& readFromDialog(istream& in, QString userInput);
    Personal& operator=(const Personal& other);
protected:
    const int nameLen, cityLen;
    char SSN[10], * name, * city;
    int year;
    long salary;
    QTextStream& writeLegibly(QTextStream&);
    friend QTextStream& operator<<(QTextStream& out, Personal& pr) {
        return pr.writeLegibly(out);
    }
};

class Student : public Personal {
public:
    Student();
    Student(char*, char*, char*, int, long, char*);
    void writeToFile(fstream&) const;
    void readFromFile(fstream&);
    int size() const {
        return Personal::size() + majorLen;
    }
    istream& readFromDialog(istream& in, QString userInput);
    Student& operator=(const Student& other);
protected:
    char* major;         // 多了一个主修课程的属性
    const int majorLen;
    QTextStream& writeLegibly(QTextStream&);
    friend QTextStream& operator<<(QTextStream& out, Student& sr) {
        return sr.writeLegibly(out);
    }
    // 不在控制台Console进行读写操作，而是在对话框Dialog
//    istream& readFromConsole(istream&);
//    friend istream& operator>>(istream& in, Student& sr) {
//        return sr.readFromConsole(in);
//    }
};

template<class T>
class Database {
public:
    Database();
    void run(QString s, int i, QString input);
    void run2(QString s, int i, QString input);
private:
    fstream database;
    QString fName;
    QTextStream& print(QTextStream&);
    void add(T&);
    bool find(const T&);
    void modify(const T&);
    friend QTextStream& operator<<(QTextStream& out, Database& db) {
        return db.print(out);
    }
};


#endif // FILE_H
